package com.example.yls.demoa;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity  implements IOnDelListenner{//implements StuAdapter.DeleteCallback
    private ListView stuListView;
    private ArrayList<Student> studentArrayList = new ArrayList<>();
    private ArrayList<Student> searchStuResultList = new ArrayList<>();
    private StuAdapter adapter;
    private Button add_btn;
    private Button search_btn;
    private EditText keyWord_edt;
    private int no = 80005;
    private boolean isResult = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        stuListView = findViewById(R.id.lv_stu);


        initStuArrayList();

        adapter = new StuAdapter(MainActivity.this, studentArrayList, MainActivity.this);
        stuListView.setAdapter(adapter);
        // ？？？？设置回应？？？？
        //adapter.setDeleteCallback(this);//自己百度的方法
        //



        setToEdtStudentClickListener();

        setToAddStudentClickListener();

        initSearchView();
    }


    /**
     * 初始化搜素框和搜素按钮
     */
    private void initSearchView() {
        search_btn = findViewById(R.id.search_btn);
        keyWord_edt = findViewById(R.id.keyWord_edt);

        search_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String keyWord = keyWord_edt.getText().toString();
                search(studentArrayList,keyWord);
                adapter.changeData(searchStuResultList);
                isResult = true;
            }
        });

        keyWord_edt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                String keyWord = keyWord_edt.getText().toString();
                search(studentArrayList,keyWord);
                adapter.changeData(searchStuResultList);
                isResult = true;
            }
        });
    }


    /**
     * 搜素方法
     * @param stuList
     * @param keyWord
     */
    private void search(ArrayList<Student> stuList, String keyWord) {
        Student s;
        searchStuResultList.clear();
        for (int i = 0; i < stuList.size(); i++) {
            if (stuList.get(i).getName().contains(keyWord)) {
                s = stuList.get(i);
                searchStuResultList.add(s);
            } else if (String.valueOf(stuList.get(i).getAge()).equals(keyWord)) {
                s = stuList.get(i);
                searchStuResultList.add(s);
            }
        }
    }


    /**
     * 跳转到添加学生页面
     */
    private void setToAddStudentClickListener() {
        add_btn = findViewById(R.id.open_add_stu);
        add_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this,addActivity.class);
                startActivityForResult(intent,10086);
            }
        });
    }



    /**
     * 点击列表目标跳转到该目标数据的修改页面
     */
    private void setToEdtStudentClickListener() {
        stuListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Student student = studentArrayList.get(i);

                if(isResult){ student = searchStuResultList.get(i); }

                Toast.makeText(MainActivity.this,
                        "姓名:"+student.getName() + ", 年龄:"
                                +  student.getAge(),
                        Toast.LENGTH_SHORT)
                        .show();
                Intent intent = new Intent();
                intent.setClass(MainActivity.this,EditActivity.class);
                intent.putExtra("NAME",student.getName());
                intent.putExtra("AGE",student.getAge());
                intent.putExtra("IMG",student.getHeadId());
                intent.putExtra("NO",student.getNo());
                startActivityForResult(intent,10086);
            }
        });
    }


    /**
     * 处理点击删除学生事件
     * @param no
     */
    @Override
    public void delIndex(int no) {
        /**
         *
         */
        Student s;
        for (int i = 0; i<studentArrayList.size(); i++){
            s = studentArrayList.get(i);
            if(s.getNo() == no){
                studentArrayList.remove(i);
            }
        }

        adapter.notifyDataSetChanged();
        Toast.makeText(MainActivity.this,
                "数据移除成功！",
                Toast.LENGTH_SHORT)
                .show();
    }



    /**
     * 处理<>返回的数据
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (data == null){
            return;
        }
        //添加学生
        if(resultCode == 10010) {
            no++;
            Student s = new Student(data.getStringExtra("NAME"),
                    data.getIntExtra("AGE", 0),
                    data.getIntExtra("IMG", R.drawable.dog1),no);
            studentArrayList.add(s);
            adapter.notifyDataSetChanged();
        }
        //编辑学生
        else if(resultCode == 20010){
            /*Student s = studentArrayList.get(data.getIntExtra("INDEX",0));
            s.setName(data.getStringExtra("NAME"));
            s.setAge(data.getIntExtra("AGE",0));
            s.setHeadId(data.getIntExtra("IMG",R.drawable.dog1));
            adapter.notifyDataSetChanged();*/

            String name = data.getStringExtra("NAME");
            int age = data.getIntExtra("AGE",0);
            int headId = data.getIntExtra("IMG",0);
            int no = data.getIntExtra("NO",0);

            for(int i = 0; i<studentArrayList.size(); i++){
                Student s = studentArrayList.get(i);

                if (s.getNo() == no){
                    s.setAge(age);
                    s.setName(name);
                    s.setHeadId(headId);
                }
            }

            String keyWord = keyWord_edt.getText().toString().trim();
            search(studentArrayList,keyWord);
            adapter.changeData(searchStuResultList);
            isResult = true;
        }
    }




    /**
     * 初始化学生列表的数据
     */
    private void initStuArrayList() {
        Student s1 = new Student("张一", 18, R.drawable.dog1,8001);
        Student s2 = new Student("张二", 19, R.drawable.dog2,8002);
        Student s3 = new Student("张三", 20, R.drawable.dog3,8003);
        Student s4 = new Student("张四", 21, R.drawable.dog4,8004);
        Student s5 = new Student("张五", 22, R.drawable.dog5,8005);

        studentArrayList.add(s1);
        studentArrayList.add(s2);
        studentArrayList.add(s3);
        studentArrayList.add(s4);
        studentArrayList.add(s5);
    }

    /*
    * 实现删除，刷新
    * */
    /*public void deletePosition(int delItemId) {
        studentArrayList.remove(delItemId);
        adapter.notifyDataSetInvalidated();
    }*/
    //自己百度的方法——————以上
}
